package com.ruoyi.web.controller.regulatory;

import cn.hutool.core.util.URLUtil;
import com.alibaba.excel.EasyExcel;
import com.ruoyi.basic.domain.JyArea;
import com.ruoyi.basic.service.JyAreaService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.regulatory.domain.JyBureau;
import com.ruoyi.regulatory.service.JyBureauService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 教体局信息Controller
 *
 * @author ruoyi
 * @date 2024-04-06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/regulatory/bureau")
public class JyBureauController extends BaseController {
    private final JyBureauService jyBureauService;
    private final JyAreaService areaService;

    /**
     * 查询教体局信息列表
     */
    @PreAuthorize("@ss.hasPermi('regulatory:bureau:list')")
    @GetMapping("/list")
    public TableDataInfo list(JyBureau jyBureau) {
        startPage();

        List<JyBureau> list = jyBureauService.selectJyBureauList(jyBureau);

        Map<Long, JyArea> longJyAreaMap = areaService.selectJyAreaMap();

        list.forEach(v -> {
            JyArea jyArea = longJyAreaMap.get(v.getAreaId());
            if (null != jyArea) {
                v.setAreaName(jyArea.getName());
            }
        });

        return getDataTable(list);
    }

    @GetMapping("/all")
    public AjaxResult all() {
        List<JyBureau> list = jyBureauService.selectJyBureauList(new JyBureau());
        return success(list);
    }

    /**
     * 导出教体局信息列表
     */
    @PreAuthorize("@ss.hasPermi('regulatory:bureau:export')")
    @Log(title = "教体局信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JyBureau jyBureau) throws IOException {
        List<JyBureau> list = jyBureauService.selectJyBureauList(jyBureau);

        // Step.3 调用AutoPoi导出Excel
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String sheetName = "教体局信息数据";
        String fileName = URLUtil.encode(sheetName).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), JyBureau.class).sheet(sheetName).doWrite(list);
    }

    /**
     * 获取教体局信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('regulatory:bureau:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jyBureauService.selectJyBureauById(id));
    }

    /**
     * 新增教体局信息
     */
    @PreAuthorize("@ss.hasPermi('regulatory:bureau:add')")
    @Log(title = "教体局信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JyBureau jyBureau) {
        return toAjax(jyBureauService.insertJyBureau(jyBureau));
    }

    /**
     * 修改教体局信息
     */
    @PreAuthorize("@ss.hasPermi('regulatory:bureau:edit')")
    @Log(title = "教体局信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JyBureau jyBureau) {
        return toAjax(jyBureauService.updateJyBureau(jyBureau));
    }

    /**
     * 删除教体局信息
     */
    @PreAuthorize("@ss.hasPermi('regulatory:bureau:remove')")
    @Log(title = "教体局信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jyBureauService.deleteJyBureauByIds(ids));
    }
}
